﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using FineUI;
using ZHDJ.Data;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.JoinParty.BLL;

namespace JoinParty.admin.ToDoFlow
{
    public partial class ToDoFlowList : PageBase
    {
        #region 属性

        public string BranchCenter
        {
            get
            {
                return Request.QueryString["BranchCenter"] ?? string.Empty;
            }
        }

        /// <summary>
        /// DataSource
        /// </summary>
        public DataSet DataSource
        {
            get
            {

                //是否组织中心
                if (string.IsNullOrEmpty(this.BranchCenter))
                {
                    //个人处理
                    return PartyJoinLinkExample.GetToDoFLow(this.txtUserName.Text.Trim(), this.CurrentUserBranchID, this.CurrentUser.LoginID);
                }
                else
                {
                    //组织处理
                    var mark = PartyOrg.QueryBranchMarkEx(this.CurrentUserBranchID);
                    //是否党委或者总支
                    if (mark == "2" || mark == "4")
                    {
                        return ProcesUtil.GetToDoFLowByBranchForUp(this.CurrentUserBranchID, IsJoinPartyAdmin);
                    }
                    else
                    {
                        return ProcesUtil.GetToDoFLowByBranch(this.CurrentUserBranchID, IsJoinPartyAdmin);
                    }
                }
            }
        }
        #endregion
        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        #endregion

        /// <summary>
        /// LoadData
        /// </summary>
        protected void LoadData()
        {
            DataSet ds = this.DataSource;
            this.dgGrid.RecordCount = ds.Tables[0].Rows.Count;
            this.dgGrid.DataSource = ds;
            this.dgGrid.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void Grid1_RowDataBound(object sender, FineUI.GridRowEventArgs e)
        {
            LinkButtonField lbBatchOperation = dgGrid.FindColumn("BatchOperation") as LinkButtonField;
            DataRowView row = e.DataItem as DataRowView;

            if (row != null)
            {
                LinkButton gridFLowOper = ControlUtil.FindControl("gridFLowOper") as LinkButton;
                LinkButton lbtnReCell = ControlUtil.FindControl("lbtnReCell") as LinkButton;

                string stepID = row["CurrentStepID"] != null ? Convert.ToString(row["CurrentStepID"]) : "";
                string standID = row["StandID"] != null ? Convert.ToString(row["StandID"]) : "";
                string BranchID = row["BranchID"] != null ? Convert.ToString(row["BranchID"]) : "";
                string AppllicationID = row["AppllicationID"] != null ? Convert.ToString(row["AppllicationID"]) : "";
                string queryParam = string.Format("?StandID={0}&ApplyBranchID={1}&CurrentStepID={2}&Audit=true", standID, BranchID, stepID);
                switch (stepID)
                {
                    case ProcesUtil.JoinStep2://登记备案
                    case ProcesUtil.JoinStep10://入党积极份子培训
                    case ProcesUtil.JoinStep13://确定发展对象
                    case ProcesUtil.JoinStep16://计划生育及社会治安等审查
                    case ProcesUtil.JoinStep17://发展对象集中培训
                    //case ProcesUtil.JoinStep18://确定入党介绍人
                    //case ProcesUtil.JoinStep19://填写入党志愿书
                    case ProcesUtil.JoinStep20://支部委员会审查
                    case ProcesUtil.JoinStep21://基层党委预审
                    case ProcesUtil.JoinStep25://组织员谈话

                    case ProcesUtil.JoinStep32://提出书面转正申请
                    case ProcesUtil.JoinStep33://党小组意见
                    //case ProcesUtil.JoinStep34://党支部意见

                    case ProcesUtil.JoinStep40://入党材料归档
                    case ProcesUtil.JoinStep6://入党积极份子报上级党委审查备案
                    case ProcesUtil.JoinStep14://报上级党委审查备案

                    case ProcesUtil.JoinStep4: //入党积极份子公示
                    case ProcesUtil.JoinStep12: //发展对象公示
                    case ProcesUtil.JoinStep24: //预备党员公示
                    case ProcesUtil.JoinStep36://预备党员转正公示
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = Dialog.GetShowReference("../FlowStep/Registration.aspx" + queryParam, "编辑");
                        lbBatchOperation.Text = "流程处理";
                        lbBatchOperation.Hidden = true;
                        break;
                    case ProcesUtil.JoinStep5://确认入党积极分子
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = Dialog.GetShowReference("../FlowStep/RegistrationStepFive.aspx" + queryParam, "编辑");
                        lbBatchOperation.Text = "流程处理";
                        break;
                    case ProcesUtil.JoinStep15://政治审查
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = Dialog.GetShowReference("../FlowStep/Examination.aspx" + queryParam, "编辑");
                        lbBatchOperation.Text = "流程处理";
                        lbBatchOperation.Hidden = true;
                        break;
                   
                        //this.lbtnReCell.Visible = true;
                        //this.lbtnBatch.Hidden = false;
                        //this.lbtnBatch.Text = "确认公示";
                        //this.lbtnBatch.ConfirmText = "是否确认公示？";
                        //break;
                    case ProcesUtil.JoinStep27://下发通知
                    case ProcesUtil.JoinStep28://公布预备党员并编入党组织

                    case ProcesUtil.JoinStep39://谈话和公布预备党员转正
                        this.lbtnBatchOper.Hidden = false;
                        this.lbtnBatchOper.OnClientClick = Dialog.GetShowReference("../FlowStep/Published.aspx" + queryParam, "编辑");

                        this.lbtnReCell.Visible = true;
                        this.gridFLowOper.Hidden = true;
                        break;
                    case ProcesUtil.JoinStep7://指定培养联系人
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = Dialog.GetShowReference("../FlowStep/AppointCultivateContacts.aspx" + queryParam+"&type=0", "编辑");
                        break;
                    case ProcesUtil.JoinStep18://指定入党介绍人
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = Dialog.GetShowReference("../FlowStep/AppointCultivateContacts.aspx" + queryParam + "&type=1", "编辑");
                        break;
                    case ProcesUtil.JoinStep8://填写入党积极分子考察表
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = WJoinStep7.GetShowReference("../FlowStep/SubActivistInvestigate.aspx" + queryParam, "编辑");
                        //gridFLowOper.DialogID = "SubActivistInvestigateDialog";
                        break;
                    case ProcesUtil.JoinStep30://填写预备党员考察表
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = DialogKaoCaBiao.GetShowReference("../FlowStep/ProPartymemberInspection.aspx" + queryParam, "编辑");
                        break;
                    case ProcesUtil.JoinStep23:/// 上报审批材料
                    case ProcesUtil.JoinStep37:/// 上报审批材料（共12份材料）
                    case ProcesUtil.JoinStep38://党委审核（3个月内）及批复
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = WShangBao2.GetShowReference("../FlowStep/Report.aspx" + queryParam, "编辑");
                        break;
                    case ProcesUtil.JoinStep9://入党积极份子培养和考察
                    case ProcesUtil.JoinStep31://预备党员教育考察
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = WJoinStep8.GetShowReference("../FlowStep/JoinPartyInspection.aspx" + queryParam, "编辑");
                        break;

                    case ProcesUtil.JoinStep3://推荐入党积极份子
                    case ProcesUtil.JoinStep11://推荐党员发展对象
                    case ProcesUtil.JoinStep22://支部大会讨论接收预备党员
                    case ProcesUtil.JoinStep26://审批预备党员
                    case ProcesUtil.JoinStep35://支部大会讨论预备党员转正
                        this.lbtnBatchOper.Hidden = true;
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = DialogMeeting.GetShowReference("../FlowStep/MeetingEx.aspx" + queryParam, "编辑");
                        //this.gridFLowOper.OnClientClick = DialogMeetingStep.GetShowReference("../FlowStep/MeetingStepEdit.aspx" + queryParam, "编辑");
                        break;


                    //this.lbtnBatch.Hidden = false;

                    //this.lbtnBatch.Text = "完成备案";
                    //this.lbtnBatch.ConfirmText = "是否确认完成备案？";
                    //}

                    case ProcesUtil.JoinStep19:
                        this.gridFLowOper.Hidden = false;
                        this.gridFLowOper.OnClientClick = wStep19.GetShowReference("../FlowStep/JoinPartyAppllication.aspx" + queryParam, "编辑");
                        lbBatchOperation.Text = "流程处理";
                        lbBatchOperation.Hidden = true;
                        break;

                    case ProcesUtil.JoinStep29://组织宣誓
                        this.lbtnBatchOper.Hidden = false;
                        this.lbtnBatchOper.OnClientClick = Dialog.GetShowReference("../FlowStep/OrganizationOath.aspx" + queryParam, "编辑");
                        break;
                    default:
                        break;
                }
            }
        }

        protected void lbtnBatch_Click(object sender, EventArgs e)
        {
            string LabelText = ((LinkButton)sender).Label;
            string applyBranchID = LabelText.Split('|')[0];
            string stepID = LabelText.Split('|')[1];
            string StandID = LabelText.Split('|')[2];
            switch (stepID)
            {
                case ProcesUtil.JoinStep4: //入党积极份子公示
                case ProcesUtil.JoinStep12: //发展对象公示
                case ProcesUtil.JoinStep24: //预备党员公示
                case ProcesUtil.JoinStep36://预备党员转正公示
                    string Message = "";///接收处理流程反馈的消息主要判断当前步骤是否已审核过
                    bool bResult = false;
                    PartyJoinLinkExample oModel = DB.PartyJoinLinkExamples.Find(StandID);
                    oModel.HandleUserID = this.CurrentUser.ID;
                    oModel.HandleUserName = this.CurrentUser.UserName;
                    oModel.HandleTime = DateTime.Now;
                    oModel.Status = (int)Enum_FlowStanceStatus.通过;
                    bResult = ProcesUtil.ApprovalProcesStep(oModel, out Message);
                    if (bResult)
                    {
                        PartyJoinApplycation app = DB.PartyJoinApplycation.Find(oModel.AppllicationID);

                        PartyJoinProcess ps = DB.PartyJoinProcesss.Find(stepID);
                        string st = ps.ProcessName;
                        string[] sArray = st.Split('、');
                        //插入关联表
                        //PartyJoinLinkExample.publicity(StandID, oModel.AppllicationID, app.LoginID, stepID, "publicity", "关于" + app.UserName + "同志" + sArray[1] + "的信息公告");
                    }
                    break;
                default:
                    ModuleUtil.ReportRecords(this.CurrentUserBranchID, applyBranchID, stepID, StandID, this.CurrentUser.LoginID, this.CurrentUser.UserName);
                    break;
            }

            this.LoadData();
            Alert.Show("成功!");
        }
        protected void lbtnReCell_Click(object sender, EventArgs e)
        {
            var l = sender as System.Web.UI.WebControls.LinkButton;
            string commnadName = l.CommandName;
            string StandID = commnadName.Split('|')[0];
            string AppllicationID = commnadName.Split('|')[1];
            string tipMessage = "";
            Back23to22(StandID);
            PartyJoinLinkExample.ReLastStep(StandID, AppllicationID, out tipMessage);
            LoadData();
            Alert.Show("撤回成功!");
        }

        private void Back23to22(string standID)
        {
            var example = DB.PartyJoinLinkExamples.Find(standID);
            if (example != null)
            {
                if (example.CurrentStepID == ProcesUtil.JoinStep28)
                {
                    var AppModel = DB.PartyJoinApplycation.Find(example.AppllicationID);
                    if (AppModel != null)
                    {
                        var oModel = DB.PartyOrgMembers.Find(AppModel.LoginID);
                        if (oModel != null)
                        {
                            oModel.runSate = "-1";
                            DB.SaveChanges();
                        }
                    }
                }
            }
        }

        protected void Dialog_Close(object sender, EventArgs e)
        {
            //LoadData();
            Response.Redirect(Request.Url.ToString());
        }

        protected void dgGrid_RowCommand(object sender, GridCommandEventArgs e)
        {

        }
    }
}